package net.hn.hnms.biz.intelligence.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

    @Primary
    @Bean(name = "dataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.master")
    public DataSourceProperties dataSourceProperties() {
                return new DataSourceProperties();
    }

    @Primary
    @Bean(name = "dataSource")
    public DataSource dataSource(@Qualifier("dataSourceProperties") DataSourceProperties dataSourceProperties) {
                return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean(name = "testDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.slave")
    public DataSourceProperties testDataSourceProperties() {
                return new DataSourceProperties();
    }

    @Bean(name = "testDataSource")
    public DataSource testDataSource(@Qualifier("testDataSourceProperties") DataSourceProperties testDataSourceProperties) {
                return testDataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean(name = "supportDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.support")
    public DataSourceProperties supportDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "supportDataSource")
    public DataSource supportDataSource(@Qualifier("supportDataSourceProperties") DataSourceProperties supportDataSourceProperties) {
        return supportDataSourceProperties.initializeDataSourceBuilder().build();
    }
}
